// pages/mine/bbs/concern/concern.js
import { HTTP } from '../../../../utils/http.js'
let ajax = new HTTP()
const app = getApp()
Page({
    data: {
        concernArr: []
    },
    onLoad(options) {
        let memberid = wx.getStorageSync("memberid")
        //获取我的关注
        this._getMyguanzhu(memberid)
    },
    gotoOther(event) {
        let nmemberid = event.currentTarget.dataset.memberid
        wx.navigateTo({
            url: '/pages/community/detail/other/other?nmemberid=' + nmemberid,
        })
    },
    // 取消关注
    onCancelTap (event) {
        let tomemberid = event.currentTarget.dataset.memberid
        wx.showModal({
            showCancel: true,
            content: '确定取消关注吗？',
            cancelText: "取消",
            cancelColor: "#000",
            confirmText: "确定",
            confirmColor: "#67c23a",
            success: (res) => {
                if (res.confirm) {
                    this._getCancelguanzhu(tomemberid)
                }
            } 
        })
    },
    // 取消关注
    _getCancelguanzhu (tomemberid) {
        ajax.request({
            url: "memfavorite",
            method: "POST",
            data: {
                memberid: app.globalData.memberid,
                tomemberid: tomemberid
            },
            success: (res) => {
                if (res.rescode === "0000") {
                    for (let i = 0; i < this.data.concernArr.length; i++) {
                        if (this.data.concernArr[i].tomemberid === tomemberid) {
                            this.data.concernArr.splice(i, 1)
                        }
                    }
                    this.setData({
                        concernArr: this.data.concernArr
                    })
                    wx.showToast({
                        title: '取关成功',
                    })
                }
            }
        })
    },
    _getMyguanzhu(nmemberid) {
        ajax.request({
            method: "GET",
            url: "mycenter",
            data: {
                actname: 'myguanzhu',
                memberid: nmemberid,
                p: 0
            },
            success: (res) => {
                console.log(res)
                if (res.rescode == "0000") {
                    this.setData({
                        concernArr: res.data
                    })
                }
            }
        })
    }
})